using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Diagnostics;
using System.IO;
using Microsoft.Xna.Framework;

namespace The_LarX
{
    public static class LanguageManager
    {
        private static LanguageSerializeData ActualLanguage = null;
        private static LanguageSerializeData EngineActualLanguage = null;

        public static Language GameLanguage;
        private static List<LanguageSerializeData> EngineLanguages = new List<LanguageSerializeData>();

        private static List<string> GameLanguagesNames = new List<string>();
        private static List<string> EngineLanguagesNames = new List<string>();

        public static string GetStringFromGame(string dataName, int index)
        {
            return GameLanguage.Languages[dataName].GetWord(index);
        }

        public static string BestLanguage;
                
        public static LanguageSerializeData LoadLanguage(string path)
        {
            string name = Path.GetFileName(path);
            return LoadLanguage(path, name);
        }
        public static LanguageSerializeData LoadLanguage(string path, string langName)
        {
            string newPath = path.Replace("[LANGUAGETAG]", BestLanguage);
            if (File.Exists(newPath))
            {
                LanguageSerializeData data = LanguageSerializer.Load(newPath);

                if (GameLanguage == null)
                {
                    Language lang = new Language();
                    lang.Lang = data.Language;
                    lang.Languages.Add(langName, data);
                    GameLanguage = lang;
                }
                else
                {
                    GameLanguage.Languages.Add(langName, data);
                }

                return data;
            }
            else
            {
                return LoadLanguage(path.Replace("[LANGUAGETAG]", "en-us"), langName);
            }
        }
        public static void RemoveLanguage(string langName)
        {
            GameLanguage.Languages.Remove(langName);            
        }

        public static string GetString(int Key)
        {
            if (ActualLanguage != null)
                return ActualLanguage.GetWord(Key);
            else
                return string.Empty;
        }
        public static string GetEngineString(int Key)
        {
            if (EngineActualLanguage != null)
                return EngineActualLanguage.GetWord(Key);
            else
                return string.Empty;
        }

        public static void Load(string langsFolder)
        {
            #region Load Engine Languages
#if WINDOWS_PHONE
            EngineLanguagesNames = LanguageSerializer.LoadListStrings(Path.Combine(Engine.Content.RootDirectory, "Engine\\Languages\\Languages.larlanguages"));
#else
            FileInfo[] infos = new DirectoryInfo(Path.Combine(Engine.Content.RootDirectory, "Engine\\Languages\\")).GetFiles("*.larlang");
            EngineLanguagesNames = new List<string>();
            for (int i = 0; i < infos.Length; i++)
            {
                string final = String.Empty;
                if (infos[i].Name.EndsWith(".larlang"))
                {
                    final = infos[i].Name.Remove(infos[i].Name.Length - 8, 8);
                }
                else
                {
                    final = infos[i].Name;
                }
                EngineLanguagesNames.Add(final);
            }
#endif

            for (int i = 0; i < EngineLanguagesNames.Count; i++)
            {
                EngineLanguages.Add(LanguageSerializer.Load(Path.Combine(Engine.Content.RootDirectory, "Engine\\Languages\\" + EngineLanguagesNames[i] + ".larlang")));
            }
            for (int i = 0; i < EngineLanguages.Count; i++)
            {
                if (CultureInfo.CurrentCulture.Name.ToLower().StartsWith(EngineLanguages[i].Language))
                {
                    EngineActualLanguage = EngineLanguages[i];
                }
            }
            if (ActualLanguage == null)
            {
                for (int i = 0; i < EngineLanguages.Count; i++)
                {
                    if (EngineLanguages[i].Language.StartsWith("en-us"))
                    {
                        EngineActualLanguage = EngineLanguages[i];
                    }
                }
                for (int i = 0; i < EngineLanguages.Count; i++)
                {
                    string country = CultureInfo.CurrentCulture.Name;
                    string Country = country.Remove(2, 3);
                    if (EngineLanguages[i].Language.StartsWith(Country))
                    {
                        EngineActualLanguage = EngineLanguages[i];
                    }
                }
            }

            #endregion

            if (!string.IsNullOrEmpty(langsFolder))
            {
                CultureInfo cul = CultureInfo.CurrentCulture;
                string FullLan = cul.Name;
                string TwoLetter = cul.TwoLetterISOLanguageName;

                DirectoryInfo[] gameLangs = new DirectoryInfo(langsFolder).GetDirectories();

                string bestAllletters = "";
                string bestTwoLetters = "";
                for (int i = 0; i < gameLangs.Length; i++)
                {
                    if (gameLangs[i].Name.ToLower() == FullLan.ToLower())
                    {
                        bestAllletters = gameLangs[i].Name;
                    }
                    if (gameLangs[i].Name.ToLower().StartsWith(TwoLetter.ToLower()))
                    {
                        bestTwoLetters = gameLangs[i].Name;
                    }
                }

                if (!string.IsNullOrEmpty(bestAllletters))
                {
                    BestLanguage = bestAllletters;
                }
                else if (!string.IsNullOrEmpty(bestTwoLetters))
                {
                    BestLanguage = bestTwoLetters;
                }
            }

            /*#region Load Game Strings

            string langsFolder = "Content\\Game\\Languages\\";

#if WINDOWS_PHONE
            GameLanguagesNames = LanguageSerializer.LoadListStrings(Path.Combine(Engine.Content.RootDirectory, "Engine\\Languages\\Languages.larlanguages"));
            if (Directory.Exists(langsFolder))
            {
                FileInfo[] infosTwo = new DirectoryInfo(langsFolder).GetFiles("*.larlang");
                GameLanguagesNames = new List<string>();
                for (int i = 0; i < infosTwo.Length; i++)
                {
                    string final = String.Empty;
                    if (infosTwo[i].Name.EndsWith(".larlang"))
                    {
                        final = infosTwo[i].Name.Remove(infos[i].Name.Length - 8, 8);
                    }
                    else
                    {
                        final = infosTwo[i].Name;
                    }
                    GameLanguagesNames.Add(final);
                }
            }

            for (int i = 0; i < GameLanguagesNames.Count; i++)
            {
                Languages.Add(LanguageSerializer.Load(Path.Combine(langsFolder, GameLanguagesNames[i] + ".larlang")));
            }
            for (int i = 0; i < Languages.Count; i++)
            {
                if (CultureInfo.CurrentCulture.Name.ToLower().StartsWith(Languages[i].Language))
                {
                    ActualLanguage = Languages[i];
                }
            }
            if (ActualLanguage == null)
            {
                for (int i = 0; i < Languages.Count; i++)
                {
                    if (Languages[i].Language.StartsWith("pt-br"))
                    {
                        ActualLanguage = Languages[i];
                    }
                }
                for (int i = 0; i < Languages.Count; i++)
                {
                    string country = CultureInfo.CurrentCulture.Name;
                    string Country = country.Remove(2, 3);
                    if (Languages[i].Language.StartsWith(Country))
                    {
                        ActualLanguage = Languages[i];
                    }
                }
            }
            */
        }
    }
}
